<template>
  <view class="avatar-popup">
    <image
      class="close-icon"
      :src="ossImg('funCard/user_close.png')"
      @click.stop="emits('close')"
    />
    <label class="title">设置你的头像和昵称</label>
    <label class="text">它们会默认展示在分享卡上 </label>
    <view class="avatar_body">
      <button
        open-type="chooseAvatar"
        @chooseavatar="onChooseAvatar"
        class="img_btn"
      >
        <view
          class="img"
          :style="{
            backgroundImage: `url(${avatarUrl})`,
          }"
        />
      </button>
      <view
        class="set"
        :style="{
          backgroundImage: `url(${ossImg('funCard/user_set.png')})`,
        }"
      ></view>
    </view>
    <view class="name_body flex">
      昵称
      <view>
        <input
          class="name_input"
          type="nickname"
          v-model="nickName"
          @focus="nameFocus()"
          @blur="nameChange($event)"
          placeholder="请输入"
        />
      </view>
    </view>
    <view
      class="btn"
      :class="{
        disabled: avatarUrl === ossImg('personal/moren_avatar.png'),
      }"
      @click.stop="setUserInfo"
    >
      确定使用
    </view>
  </view>
</template>

<script setup>
import { defineProps, ref, defineEmits, watch } from "vue";
import { message, ossImg } from "@/utils/utils";
import { updateInfo } from "@/services/biscuit";

const emits = defineEmits(["confirm", "close", "skuChange"]);
const props = defineProps({
  userInfo: {
    type: Object,
    default: () => ({}),
  },
});
const nickName = ref("");
const avatarUrl = ref("");

watch(
  () => props.userInfo,
  () => {
    console.log("监听porps", props.userInfo);
    nickName.value = props.userInfo.nickname;
    avatarUrl.value =
      props.userInfo.icon || ossImg("personal/moren_avatar.png");
  },
  {
    immediate: true, // 这个属性是重点啦
  }
);

const onChooseAvatar = (e) => {
  uni.showLoading({ title: "上传中" });
  uni.uploadFile({
    url: process.env.BASE_API + "s-content/api/material/upload", //服务器地址
    fileType: "image",
    filePath: e.detail.avatarUrl,
    name: "file",
    formData: {},
    header: {
      "content-type": "multipart/form-data",
      Authorization: `Bearer ${uni.getStorageSync("TOKEN")}`,
    },
    success: (uploadFileRes) => {
      avatarUrl.value = JSON.parse(uploadFileRes.data).data;
    },
    complete: (res) => {
      uni.hideLoading();
    },
  });
  getApp().emdlz2("track", "event", "fc_choose_card", "click", "fc_avatar", "");
};
const nameFocus = () => {
  getApp().emdlz2("track", "event", "fc_choose_card", "click", "fc_name", "");
};
const nameChange = (e) => {
  if (e.detail.value === "") return message("昵称不能为空");
  nickName.value = e.detail.value;
};

const setUserInfo = () => {
  getApp().emdlz2(
    "track",
    "event",
    "fc_choose_card",
    "click",
    "fc_confirm_use",
    ""
  );
  if (nickName.value === "") {
    return message("昵称不能为空");
  } else if (nickName.value === "微信用户") {
    return message("请设置昵称");
  }
  if (avatarUrl.value === ossImg("personal/moren_avatar.png")) return;
  return new Promise(async (resolve, reject) => {
    const res = await updateInfo({
      nickname: nickName.value,
      icon: avatarUrl.value,
    });
    if (res?.code === 200) {
      message("修改成功");
      resolve(res);
      emits("close");
    } else {
      message(res.message);
      reject(res.message);
    }
  });
};
</script>

<style lang="less" scoped>
@import "@/static/css/theme.less";

.avatar-popup {
  position: relative;
  height: 301px;
  box-sizing: border-box;
  padding: 20px 15px 0;
  font-family: Source Han Sans CN;
  font-weight: 500;
  .close-icon {
    position: absolute;
    top: 20px;
    left: 21px;
    width: 14px;
    height: 14px;
  }
  .title,
  .text {
    display: block;
    padding-left: 30px;
  }
  .title {
    height: 16px;
    line-height: 16px;
    font-size: 16px;
    color: #212121;
    margin-bottom: 5px;
  }
  .text {
    font-size: 11px;
    line-height: 11px;
    color: #7f7f7f;
  }
  .avatar_body {
    position: relative;
    width: 80px;
    height: 80px;
    border-radius: 50%;
    margin: 25px auto;
    .img_btn {
      padding: 0;
      border-radius: 50%;
    }
    .img {
      width: 80px;
      height: 80px;
      border-radius: 50%;
      background-repeat: no-repeat;
      background-position: top;
      background-size: cover;
    }
    .set {
      position: absolute;
      left: 0;
      bottom: 0;
      z-index: 2;
      background-repeat: no-repeat;
      background-position: bottom left;
      background-size: 80px 21px;
      width: 100%;
      height: 21px;
    }
  }
  .name_body {
    width: 348px;
    height: 40px;
    line-height: 40px;
    border-bottom: 1px solid #e9e9e9;
    margin: 25px auto;
    box-sizing: border-box;
    padding: 0 5px;
    .name_input {
      height: 40px;
      line-height: 40px;
      margin-left: 8px;
    }
  }
  .btn {
    position: absolute;
    left: 16px;
    bottom: 9px;
    width: 343px;
    height: 40px;
    line-height: 40px;
    text-align: center;
    background: #0044c9;
    border-radius: 20px;
    font-size: 16px;
    color: #fff;
    &.disabled {
      background-color: #f6f6f6;
      color: #adadad;
    }
  }
}
</style>
